<?php

namespace catchAdmin\archives\excel;

use app\common\model\PatientInfo;
use catcher\library\excel\ExcelContract;

class LostExport implements ExcelContract
{
    /**
     * 设置头部
     *
     * @time 2020年09月08日
     * @return string[]
     */
    public function headers(): array
    {

        // TODO: Implement headers() method.
        return [
            '病案号','姓名','性别','民族','联系电话','主治医师','病种','疾病诊断','末次随访时间','末次随访','失访时间','失访原因'
        ];
    }

    /**
     * 处理数据
     *
     * @time 2020年09月08日
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @return array
     */
    public function sheets(): array
    {
        $req = request()->param();
        $where[] = ['lost_access', '=', 1];
        if (isset($req['patientId']) && !empty($req['patientId'])) {
            $where[] = ['patientId', 'in', $req['patientId']];
        }

        $field = 'medicalRecordsNo,name,sex,nation,presentTelphone,inList.inchargeDoctorName,bz,inList.diagnose,follow_node,lost_access_result,lost_access_date';

        $data = (new PatientInfo())->mongoSearch([],[],['patientId'])
            ->field($field)
            ->where($where)
            ->order('lost_access_date', 'desc')
            ->select()
            ->toArray();
        $ret = [];
        foreach ($data as $k => $v) {
            $bingzhong_string = '';
            if (isset($v['bz']['bingzhongArr'])) {
                $bingzhong_array = array_column($v['bz']['bingzhongArr'], 'bingzhong_name');
                $bingzhong_string = implode(',', $bingzhong_array);
            }

            $ret[] = [
                'medicalRecordsNo' => $v['medicalRecordsNo'],
                'name' => $v['name'],
                'sex' => $v['sex'],
                'nation' => $v['nation'],
                'presentTelphone' => $v['presentTelphone'],
                'inchargeDoctorName' => $v['inList']['inchargeDoctorName'],
                'bz' => $bingzhong_string,
                'diagnose' => $v['inList']['diagnose'],
                'follow_node_time' => $v['follow_node']['follow_time'],
                'follow_node_result' => $v['follow_node']['resultName'],
                'lost_access_date' => $v['lost_access_date'],
                'lost_access_result' => $v['lost_access_result']
            ];
        }
        return $ret;
    }

    public function setRow(): int
    {
        return 1;
    }
}